package com.lg.system.wxmessage.service.impl;

import com.lg.system.wxmessage.entity.WxMessage;
import com.lg.system.wxmessage.dao.WxMessageDao;
import com.lg.system.wxmessage.service.WxMessageService;
import com.lg.wechat.business.wxuser.entity.WxUser;
import com.lg.wechat.business.wxuser.service.WxUserService;

import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.WxMpMassOpenIdsMessage;
import me.chanjar.weixin.mp.bean.WxMpMassPreviewMessage;
import me.chanjar.weixin.mp.bean.result.WxMpMassSendResult;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import java.util.List;
import java.util.Arrays;
import java.util.Date;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dl
 * @since 2018-04-12
 */
@Transactional
@Service
public class WxMessageServiceImpl extends ServiceImpl<WxMessageDao, WxMessage> implements WxMessageService {

	@Autowired
	WxMpService wxMpService;
	
	@Autowired
	WxUserService wxUserService;
	
	@Override
	public boolean save( WxMessage wxMessage) throws WxErrorException {
		//备注 测试号不支持发送图文消息，值能发送文本，单微信文档上并没说名
		wxMessage.setGmtCreate(new Date());
		String uid[]=wxMessage.getUserId().split(",");
		List<WxUser> wxUsers= wxUserService.selectBatchIds(Arrays.asList(uid));
		if(!wxUsers.isEmpty()){
			if(wxMessage.getState()==1){//正式发送
				WxMpMassOpenIdsMessage massMessage = new WxMpMassOpenIdsMessage();
				if(WxConsts.MassMsgType.TEXT.equals(wxMessage.getMsgType())){
					massMessage.setContent(wxMessage.getContent());
				}
				massMessage.setMsgType(wxMessage.getMsgType());
				if("video".equals(wxMessage.getMsgType())){
					massMessage.setMsgType(WxConsts.MassMsgType.MPVIDEO);
				}
				for(WxUser wxUser:wxUsers){
					massMessage.getToUsers().add(wxUser.getOpenId());
				}
				WxMpMassSendResult massResult = wxMpService.getMassMessageService().massOpenIdsMessageSend(massMessage);
				if(massResult.getErrorCode().equals("0")){
					return super.insert(wxMessage);
				}
			}else{//预览
				WxMpMassPreviewMessage previewMessage=new WxMpMassPreviewMessage(); 
				if(WxConsts.MassMsgType.TEXT.equals(wxMessage.getMsgType())){
					previewMessage.setContent(wxMessage.getContent());
				}
				previewMessage.setMediaId(wxMessage.getMediaKey());
				previewMessage.setMsgType(wxMessage.getMsgType());
				previewMessage.setToWxUserOpenid(wxUsers.get(0).getOpenId());
                if("video".equals(wxMessage.getMsgType())){
                	previewMessage.setMsgType(WxConsts.MassMsgType.MPVIDEO);
				}
				WxMpMassSendResult massResult = wxMpService.getMassMessageService().massMessagePreview(previewMessage);
				if(massResult.getErrorCode().equals("0")){
					return true;
				}
			}
		}
		return false;
	}

	@Override
	public boolean update(WxMessage wxMessage) {
		wxMessage.setGmtModified(new Date());
		return super.updateById(wxMessage);
	}
	
	
	@Override
	public boolean deleteById(Long id) {
		return super.deleteById(id);
	}

	@Override
	public boolean deleteByIds(List<Long> ids) {
		if(null==ids||ids.isEmpty()){
			return false;
		}
		return super.deleteBatchIds(ids);
	}

}
